<?php
class db_query extends database
{
	public static function newInstance()
	{
		return new self();
	}
	public function insert($tabla,$array_datos,$other = null,$escaped = false)
	{
		$campos = $valores = array();
		$esc = $escaped != false ? true :false;
		foreach($array_datos as $llave => $valor)
		{
			$campos[] = $llave;
			
			if($esc)
			{
				$valores[] = database::_($valor);
			}
			else
			{
				$valores[] ="'".  strtr($valor,array("'" =>"\\'")) ."'";
			}
			
			$s = ' , ';
		}
		$query = 'INSERT INTO `'.$tabla.'` (`'. implode('`,`',$campos) .'`) VALUES ('. implode(",",$valores) .')';
		$query = is_null($other) ?$query : $query . ' ' .$other;

		//if(is_array($escaped) && null != $other)
		if(is_array($escaped))
		{
			/*$other = strtr($other,array('%' => '%%','::' => '--scaped--:',':'=>'%'));
			$other = vsprintf($other,array_map('database::_',$escaped));
			*/
			$query = self::escape($query ,$escaped);
		}
		//$query = 'INSERT INTO `'.$tabla.'` (`'. implode('`,`',$campos) .'`) VALUES ('. implode(",",$valores) .')';
		parent::query($query);
		return $this;
	}
	static public function escape($sql,$array_assoc)
	{
		if(isset($array_assoc[0]))
		{
			$sql = strtr($sql,array('%' => '%%','::' => '--scaped--:',':'=>'%'));
			$sql = vsprintf($sql,array_map('database::_',$array_assoc));
		}
		else
		{
			$other = strtr($other,array('::' => '--scaped-:-'));
			foreach($array_assoc as $key => $value)
			{
				$sql = strtr($sql,array( ':'.$key => parent::_($value)));
			}
			
		}
		$sql = strtr($sql,array('--scaped-:-'=>':'));
		return $sql;
	}
	public function delete($tabla,$other = null,$escaped = false)
	{
		$query = 'DELETE FROM '.$tabla;
		
		/*if(is_array($escaped) && null != $other)
		{
			$other = strtr($other,array('%' => '%%',':'=>'%'));
			$other = vsprintf($other,array_map('database::_',$escaped));
		}*/
		//parent::query($query.' '.$other);
		$query = is_null($other) ?$query : $query . ' ' .$other;
		if(is_array($escaped))
		{
			$query = self::escape($query ,$escaped);
		}
		parent::query($query);
		return $this;
	}
	public function update($tabla,$array_datos,$other = null,$escaped = false)
	{
		$query = 'UPDATE '.$tabla. ' SET ' ;
		if(is_array($array_datos))
		{
			$s = '';$esc = $escaped != false ? true :false;
			foreach($array_datos as $llave => $valor)
			{
				if($esc)
				{
					$query .= "$s`$llave`='".  database::_($valor) ."'";
				}
				else
				{
					$query .= "$s`$llave`='".  strtr($valor,array("'","\\'")) ."'";
				}
				
				$s = ' , ';
			}

			$set = $new_set;
		}
		/*if(is_array($escaped) && null != $other)
		{
			$other = strtr($other,array('%' => '%%',':'=>'%'));
			$other = vsprintf($other,array_map('database::_',$escaped));
		}
		parent::query($query.' '.$other);
		*/
		$query = is_null($other) ?$query : $query . ' ' .$other;
		if(is_array($escaped))
		{
			$query = self::escape($query ,$escaped);
		}
		parent::query($query);
		return $this;
	}
	public function select($tabla,$rows,$other = null,$escaped = null)
	{
		$query = 'SELECT '.implode(',',$rows).' FROM '.$tabla;
		/*if(is_array($escaped))
		{
			$query = strtr($query,array('%' => '%%',':'=>'%'));
			$query = vsprintf($query,array_map('database::_',$escaped));
			//WHERE user='%s' AND password='%s'
		}
		parent::query($query);/**/
		$query = is_null($other) ?$query : $query . ' ' .$other;
		if(is_array($escaped))
		{
			$query = self::escape($query,$escaped);
		}
		parent::query($query);
		return $this;
	}
	public function free_result()
	{
		parent::free_result();
		return $this;
	}
}